define(function (require, exports, module) {
    var angular = require("angular");
    var ui = require('ui');
    var util = require('util');
    require("angular-ui");
    var common = require("./common");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/para-setting/"; //api前缀

    angular.module('PublicRegistrationReview', [])
        .controller('RegistrationReviewController', ['$scope', '$http', function ($scope, $http) {
            $scope.save = function () {
                var data = $scope.data;
                $http.post(
                        api_prefix + "save-reg-audit",
                    {
                        orgBrokerAutoAudit: data.agencyregisterautocheck,
                        brokerAutoAudit: data.regisautocheck,
                        regbrokerAudit: data.regbrokerAudit,
                        isUploadDocument: data.isUploadDocument,
                        regLoginSms: data.isSms.regLogin.iseable,
                        regLoginTem: data.isSms.regLogin.template,
                        forgetTem: data.isSms.forget.template,
                        brokerRegSms: data.brokerRegRemind.IsEnable,
                        smsId: data.brokerRegRemind.id,
                        mobile: data.brokerRegRemind.Mobiles.join(';') + ";",
                        startTime: $scope.startTimeHour + ':' + $scope.startTimeMinute,
                        endTime: $scope.endTimeHour + ':' + $scope.endTimeMinute
                    }
                )
                    .success(function () {
                        ui.showSucTip();
                    })
            };
            //添加手机号
            $scope.addTel = function () {
                common.showMobileInputDialog({
                    ok: function (mobile) {
                        var data = $scope.data;
                        $http.post(
                                api_prefix + "add-reg-remind",
                            {
                                id: data.brokerRegRemind.id,
                                mobile: mobile
                            }
                        )
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    data.brokerRegRemind.Mobiles.push(mobile);
                                    ui.showSucTip();
                                }
                            });
                    }
                });
            };
            //删除短信注册提醒
            $scope.delTel = function (mobile, index) {
                util.confirm("您确定要删除吗？", {
                    ok: function () {
                        var data = $scope.data;
                        $http.post(
                                api_prefix + "del-reg-remind",
                            {
                                type: 0,
                                mobile: mobile
                            }
                        )
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    data.brokerRegRemind.Mobiles.splice(index, 1);
                                    ui.showSucTip();
                                }
                            });
                    }
                });
            };

            //发送短信
            $scope.sendSMs = function (mobile,message,type) {
                util.confirm("您确定发送测试短信吗？", {
                    ok: function () {
                        //校验模板信息
                        if(message.indexOf('{$code}') == -1){
                            util.alert('短信模板信息不包含{$code},请重新填写');
                            return false;
                        }
                        message = message.replace('{$code}',parseInt(Math.random()*1000000));
                        var api_prefix2 = G_CONFIG["api_prefix"] + "common/sms/";
                        $http.post(
                            api_prefix2 + "send-verification-code",
                            {
                                message: message,
                                phone: mobile,
                                type:type,
                                organizationId: $scope.data.organizationId
                            }
                        )
                            .success(function (res) {
                                if (res.retCode == 0) {
                                    ui.showSucTip('发送成功');
                                }
                            });
                    }
                });
            };

            //判断是否全选经纪人类型
            var isSelectedAll = function(list){
                var selectedAll = true;
                if(list && list.length>0){
                    for(var i = 0,item;item = list[i];i++){
                        if (!item.selected) {
                            selectedAll = false;
                            break;
                        }
                    }
                }
                return selectedAll;
            };

            //自动审核经纪人类型全选
            $scope.updateRegbrokerAuditCheckAll = function(){
                var data = $scope.data;
                if(data){
                    var regbrokerAudit = data.regbrokerAudit;
                    var selectedAll = regbrokerAudit.selectedAll;
                    for(var i = 0,item;item = regbrokerAudit[i];i++){
                        item.selected = selectedAll;
                        if(selectedAll){
                            item.myParamValue = "1";
                        }else{
                            item.myParamValue = "0";
                        }
                    }
                }
            };

            //自动审核经纪人类型
            $scope.updateRegbrokerAuditCheck = function(record){
                if(record.selected){
                    record.myParamValue = "1";
                }else{
                    record.myParamValue = "0";
                }
                var regbrokerAudit = $scope.data.regbrokerAudit;
                regbrokerAudit.selectedAll = isSelectedAll(regbrokerAudit);
            };

            $http.get(api_prefix + "get-reg-audit")
                .then(function (res) {
                    var data = res.data.data;

                    //机构经纪人注册自动审核
                    var agencyRegisterAutoCheck = data.agencyregisterautocheck;
                    ui.initSwitch('#agencyregister_autocheck', function (data) {
                        $scope.data.agencyregisterautocheck = data.value;
                    });
                    if (agencyRegisterAutoCheck == '1') {
                        ui.openSwitch("agencyregister_autocheck");
                    }
                    else {
                        ui.offSwitch("agencyregister_autocheck");
                    }
                    //经纪人注册自动审核
                    //var registAutoCheck = data.regisautocheck;
                    //ui.initSwitch('#regist_autocheck', function (data) {
                    //    $scope.data.regisautocheck = data.value;
                    //});
                    //if (registAutoCheck == '1') {
                    //    ui.openSwitch("regist_autocheck");
                    //}
                    //else {
                    //    ui.offSwitch("regist_autocheck");
                    //}

                    //经纪人注册自动审核
                    var regbrokerAudit = data.regbrokerAudit;
                    var brokerItem;
                    for(var i = 0;brokerItem = regbrokerAudit[i];i++){
                        if(brokerItem.myParamValue === "1"){
                            brokerItem.selected = true;
                        }else{
                            brokerItem.selected = false;
                        }
                    }
                    regbrokerAudit.selectedAll = isSelectedAll(regbrokerAudit);

                    //上传资质文档
                    var isUploadDocument = data.isUploadDocument;
                    ui.initSwitch('#upload_document', function (data) {
                        $scope.data.isUploadDocument = data.value;
                        $scope.$applyAsync();
                    });
                    if (isUploadDocument == 1) {
                        ui.openSwitch("upload_document");
                    }
                    else {
                        ui.offSwitch("upload_document");
                    }

                    //注册/登录短信验证
                    var isSmsRegLogin = data.isSms.regLogin;

                    ui.initSwitch('#reg_sms', function (data) {
                        $scope.data.isSms.regLogin.iseable = data.value;
                        $scope.$applyAsync();
                    });
                    if (isSmsRegLogin.iseable == 1) {
                        ui.openSwitch("reg_sms");
                    }
                    else {
                        ui.offSwitch("reg_sms");
                    }
                    //新经纪人注册提醒短信
                    var brokerRegRemind = data.brokerRegRemind;
                    ui.initSwitch('#broker_reg_remind', function (data) {
                        $scope.data.brokerRegRemind.IsEnable = data.value;
                        $scope.$applyAsync();
                    });
                    if (brokerRegRemind.IsEnable == '1') {
                        ui.openSwitch('broker_reg_remind');

                    } else {
                        ui.offSwitch('broker_reg_remind');
                    }

                    var hours = [];
                    for (var i = 0; i < 24; i++) {
                        hours.push(util.pad(i, 2));
                    }
                    $scope.hours = hours;
                    $scope.minutes = ['00', '10', '20', '30', '40', '50', '59'];

                    //发送时间段
                    var split = brokerRegRemind.StartTime.split(':');
                    $scope.startTimeHour = split[0];
                    $scope.startTimeMinute = split[1];

                    split = brokerRegRemind.EndTime.split(':');
                    $scope.endTimeHour = split[0];
                    $scope.endTimeMinute = split[1];

                    var mobiles = brokerRegRemind.Mobiles;
                    mobiles = mobiles.replace(/;$/, '');
                    if (mobiles == "") {
                        brokerRegRemind.Mobiles = [];
                    }
                    else {
                        brokerRegRemind.Mobiles = mobiles.split(';');
                    }

                    $scope.data = data;

                })

        }])
});